package com.luiz.aero.android.sensor;

import com.luiz.aero.android.data.StaticPositionData;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSListener {
	
	private Location aeroLocation;
	private TextView gpsTx = null;
	private TextView isGPSEnabled = null;
	
	
	
	
	public GPSListener(TextView location, TextView isGPSEnabled) {
		super();
		this.gpsTx = location;
		this.isGPSEnabled = isGPSEnabled;
		
	}




	public void setGPSConnection(Context context) {
		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
			public void onLocationChanged(Location location) {
				// Called when a new location is found by the network location provider.
				aeroLocation = location;
				String locationString = "Latitude: " + aeroLocation.getLatitude() +"Longitude: " + aeroLocation.getLongitude()+"Altitude: " + aeroLocation.getAltitude();
				gpsTx.setText(locationString);
				StaticPositionData.location = aeroLocation;
			}

			public void onStatusChanged(String provider, int status, Bundle extras) {
				isGPSEnabled.setText("GPS: Provider " + provider + " Status:" + status);
				
			}

			public void onProviderEnabled(String provider) {
				isGPSEnabled.setText("GPS: Provider " + provider + " Enabled");
			}

			public void onProviderDisabled(String provider) {
				isGPSEnabled.setText("GPS: Provider " + provider + " Disabled");
			}
		};

		// Register the listener with the Location Manager to receive location updates
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

	}

}
